package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import sim.Blob;
import sim.World;

public class ControlsPanel extends JPanel implements ActionListener, BlobSelectionListener
{
	private final JLabel speedLabel;
	private final JButton speedupButton;
	private final JButton speeddownButton;
	private final JButton pauseResumeButton;
	private double speed = 1.0;
	private boolean isPaused = false;
	private final JTextArea blobStats;
	private final World world;
	
	public ControlsPanel(World world)
	{
		this.world = world; 
		speedLabel = new JLabel(""+speed);
		speedupButton = new JButton("Speed +");
		speeddownButton = new JButton("Speed -");
		pauseResumeButton = new JButton("Pause");
		speedupButton.addActionListener(this);
		speeddownButton.addActionListener(this);
		pauseResumeButton.addActionListener(this);
		
		blobStats = new JTextArea("Nothing selected");
		blobStats.setOpaque(true);
		blobStats.setBackground(new Color(150,30,210));
		blobStats.setForeground(new Color(255,255,255));
		buildGui();
	}

	@Override
	public void actionPerformed(ActionEvent event) 
	{
		if(event.getSource()==speedupButton)
		{
			speed *= 1.2;
			speedLabel.setText(""+speed);
		}
		else if(event.getSource()==speeddownButton)
		{
			speed /= 1.2;	
			speedLabel.setText(""+speed);
		}
		else if(event.getSource() == pauseResumeButton)
		{
			if(isPaused)
			{
				isPaused = false;
				pauseResumeButton.setText("Pause");
			}
			else
			{
				isPaused = true;
				pauseResumeButton.setText("Resume");
			}
		}
		if(isPaused)
		{
			world.setTimeFlowSpeed(0.0);
		}
		else
		{
			world.setTimeFlowSpeed(speed);
		}
	}
	
	@Override
	public void blobSelectionChanged(Blob blob) 
	{
		if(blob == null)
		{
			blobStats.setText("Nothing selected");
		}
		else
		{
			String text = "";
			text += "Area: " + blob.getArea() + "\n";
			text += "Radius: " + blob.getRadius() + "\n";
			text += "Reproduction Area:  " + blob.getAI().getReproductionArea();
			blobStats.setText(text);
		}
	}

	private void buildGui() 
	{
		setLayout(new GridBagLayout());
	    GridBagConstraints gbc = new GridBagConstraints();
	    gbc.fill = GridBagConstraints.HORIZONTAL;
	    gbc.gridx = 0;
	    gbc.gridy = 0;
	    gbc.weightx = 0;
	    gbc.weighty = 1;
	    add(new JLabel("Speed:"),gbc);
	    
	    gbc.gridx=1;
		//No way to just set preferred width!
		speedLabel.setPreferredSize(new Dimension(60,speedLabel.getPreferredSize().height));
	    add(speedLabel,gbc);
	    
	    gbc.gridx = 0;
	    gbc.gridy = 1;
	    gbc.gridwidth = 2;
	    add(speedupButton,gbc);
	    
	    gbc.gridx = 0;
	    gbc.gridy = 2;
	    gbc.gridwidth = 2;
	    add(speeddownButton,gbc);

	    gbc.gridx = 0;
	    gbc.gridy = 3;
	    gbc.gridwidth = 2;
	    add(pauseResumeButton,gbc);
	    
	    gbc.gridx = 2;
	    gbc.gridy = 0;
	    gbc.gridheight = 4;
	    gbc.gridwidth = 1;
	    gbc.weightx = 1;
	    gbc.fill = GridBagConstraints.BOTH;
	    add(blobStats,gbc);
	    
	}
}
